In [1]:
import pandas as pd
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
In [2]:
df = pd.read_csv('./data/cansim-0800020-eng-6674700030567901031.csv',
skiprows=6, skipfooter=9,
engine='python')
df.head()
Out[2]:
In [3]:
from pandas.tseries.offsets import MonthEnd
In [4]:
df['Adjustments'] = pd.to_datetime(df['Adjustments']) + MonthEnd(1)
df = df.set_index('Adjustments')
df.head()
Out[4]:
In [5]:
df.plot(figsize = (12,7))
Out[5]:
In [6]:
split_date = pd.Timestamp('01-01-2011')
In [7]:
train = df.loc[:split_date, ['Unadjusted']]
test = df.loc[split_date:, ['Unadjusted']]
In [8]:
ax = train.plot()
test.plot(ax = ax)
plt.legend(['train', 'test'])
Out[8]:
In [9]:
from sklearn.preprocessing import MinMaxScaler
sc = MinMaxScaler()
# Tranforming only on the train data
# Fit to it on the test data
train_sc = sc.fit_transform(train)
test_sc = sc.transform(test)
In [10]:
train_sc[:4]
Out[10]:
In [11]:
X_train = train_sc[:-1]
y_train = train_sc[1:]
X_test = test_sc[:-1]
y_test = test_sc[1:]
In [12]:
from keras.models import Sequential
from keras.layers import Dense
import keras.backend as K
from keras.callbacks import EarlyStopping
In [13]:
K.clear_session()
model = Sequential()
model.add(Dense(12,
input_dim = 1,
activation = 'relu'))
model.add(Dense(1))
model.compile(optimizer = 'adam',
loss = 'mean_squared_error',
metrics = ['mae'])
model.summary()
In [14]:
early_stop = EarlyStopping(monitor = 'loss',
patience = 10,
verbose = 1)
In [15]:
model.fit(X_train,
y_train,
epochs = 200,
batch_size = 2,
verbose = 2,
callbacks = [early_stop])
Out[15]:
In [16]:
y_pred = model.predict(X_test)
In [17]:
plt.plot(y_test)
plt.plot(y_pred)
Out[17]:
In [18]:
from keras.layers import LSTM
In [19]:
X_train.shape
Out[19]:
In [20]:
#3D tensor with shape (batch_size, timesteps, input_dim)
X_train[:, None].shape
Out[20]:
In [21]:
X_train_t = X_train[:, None]
X_test_t = X_test[:, None]
In [22]:
K.clear_session()
model = Sequential()
model.add(LSTM(6,
input_shape = (1, 1)))
model.add(Dense(1))
model.compile(optimizer = 'adam',
loss = 'mean_squared_error',
metrics = ['mae'])
In [23]:
model.summary()
In [24]:
early_stop = EarlyStopping(monitor = 'loss',
patience = 10,
verbose = 1)
In [25]:
model.fit(X_train_t,
y_train,
epochs = 100,
batch_size = 1,
verbose = 2,
callbacks=[early_stop])
Out[25]:
In [26]:
y_pred = model.predict(X_test_t)
plt.plot(y_test)
plt.plot(y_pred)
Out[26]:
In [27]:
train_sc.shape
Out[27]:
In [28]:
train_sc_df = pd.DataFrame(train_sc,
columns = ['Scaled'],
index = train.index)
test_sc_df = pd.DataFrame(test_sc,
columns = ['Scaled'],
index = test.index)
train_sc_df.head()
Out[28]:
In [29]:
for s in range(1, 13):
train_sc_df['shift_{}'.format(s)] = train_sc_df['Scaled'].shift(s)
test_sc_df['shift_{}'.format(s)] = test_sc_df['Scaled'].shift(s)
In [30]:
train_sc_df.head(13)
Out[30]:
In [31]:
X_train = train_sc_df.dropna().drop('Scaled', axis=1)
y_train = train_sc_df.dropna()[['Scaled']]
X_test = test_sc_df.dropna().drop('Scaled', axis=1)
y_test = test_sc_df.dropna()[['Scaled']]
In [32]:
X_train.head()
Out[32]:
In [33]:
X_train.shape
Out[33]:
In [34]:
X_train = X_train.values
X_test= X_test.values
y_train = y_train.values
y_test = y_test.values
In [35]:
K.clear_session()
model = Sequential()
model.add(Dense(12,
input_dim = 12,
activation = 'relu'))
model.add(Dense(1))
model.compile(optimizer = 'adam',
loss = 'mean_squared_error',
metrics = ['mae'])
model.summary()
In [36]:
early_stop = EarlyStopping(monitor = 'loss',
patience = 10,
verbose = 1)
In [37]:
model.fit(X_train,
y_train,
epochs = 200,
batch_size = 1,
verbose = 2,
callbacks = [early_stop])
Out[37]:
In [38]:
y_pred = model.predict(X_test)
plt.plot(y_test)
plt.plot(y_pred)
Out[38]:
In [39]:
X_train_t = X_train.reshape(X_train.shape[0], 1, 12)
X_test_t = X_test.reshape(X_test.shape[0], 1, 12)
In [40]:
X_train_t.shape
Out[40]:
In [41]:
K.clear_session()
model = Sequential()
model.add(LSTM(6,
input_shape = (1, 12)))
model.add(Dense(1))
model.compile(optimizer = 'adam',
loss = 'mean_squared_error',
metrics = ['mae'])
In [42]:
model.summary()
In [43]:
early_stop = EarlyStopping(monitor = 'loss',
patience = 10,
verbose = 1)
In [44]:
model.fit(X_train_t,
y_train,
epochs = 100,
batch_size = 1,
verbose = 2,
callbacks = [early_stop])
Out[44]:
In [45]:
y_pred = model.predict(X_test_t)
plt.plot(y_test)
plt.plot(y_pred)
Out[45]:
In the model above we reshaped the input shape to: (num_samples, 1, 12), i.e. we treated a window of 12 months as a vector of 12 coordinates that we simultaneously passed to all the LSTM nodes. An alternative way to look at the problem is to reshape the input to (num_samples, 12, 1). This means we consider each input window as a sequence of 12 values that we will pass in sequence to the LSTM. In principle this looks like a more accurate description of our situation. But does it yield better predictions? Let's check it.
X_train and X_test so that they represent a set of univariate sequencesinput_shape
In [46]:
X_train.shape, X_test.shape, y_train.shape, y_test.shape
Out[46]:
In [47]:
print(X_train[0])
print(X_train[1]),
print(y_train[0])
In [48]:
X_train = X_train.reshape(-1, 12, 1)
X_test = X_test.reshape(-1, 12, 1)
In [49]:
print(X_train[0])
In [50]:
X_train.shape, X_test.shape, y_train.shape, y_test.shape
Out[50]:
In [51]:
from keras.models import Model
from keras.layers import Input, LSTM
import keras.backend as K
K.clear_session()
inp = Input(shape = (12, 1))
net = LSTM(units = 6)(inp)
prediction = Dense(units = 1)(net)
model = Model(inputs = inp, outputs = prediction)
model.compile(optimizer = 'adam',
loss = 'mean_squared_error',
metrics = ['mae'])
In [52]:
model.summary()
In [53]:
early_stop = EarlyStopping(monitor = 'loss',
patience = 10,
verbose = 1)
In [54]:
model.fit(X_train,
y_train,
epochs = 500,
batch_size = 20,
verbose = 2,
callbacks = [early_stop])
Out[54]:
In [55]:
y_pred = model.predict(X_test)
plt.plot(y_test)
plt.plot(y_pred)
Out[55]:
RNN models can be applied to images too. In general we can apply them to any data where there's a connnection between nearby units. Let's see how we can easily build a model that works with images.
(feel free to run this exercise on a cloud GPU if it's too slow on your laptop)
In [56]:
from keras.datasets import mnist
In [57]:
(X_train, Y_train), (X_test, Y_test) = mnist.load_data(path = 'mnist.npz')
In [58]:
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
# Regularization
X_train /= 255.0
X_test /= 255.0
In [59]:
from keras.utils import to_categorical
In [60]:
Y_train.shape
Out[60]:
In [61]:
Y_train_cat = to_categorical(Y_train)
Y_test_cat = to_categorical(Y_test)
In [62]:
Y_train_cat.shape, Y_test_cat.shape
Out[62]:
In [63]:
X_train = X_train.reshape(X_train.shape[0], -1, 1)
X_test = X_test.reshape(X_test.shape[0], -1, 1)
In [64]:
X_train.shape, X_test.shape
Out[64]:
In [65]:
from keras.models import Model
from keras.layers import Input, LSTM, Dense
import keras.backend as K
In [66]:
K.clear_session()
In [67]:
inp = Input(shape = X_train.shape[1:])
In [68]:
net = LSTM(units = 32)(inp)
prediction = Dense(units = 10,
activation = 'softmax')(net)
model = Model(inputs = inp,
outputs = prediction)
model.compile(optimizer = 'adam',
loss = 'categorical_crossentropy',
metrics = ['accuracy'])
In [69]:
model.summary()
In [ ]:
early_stop = EarlyStopping(monitor = 'loss',
patience = 1,
verbose = 1)
In [ ]:
model.fit(X_train,
Y_train_cat,
epochs = 2,
batch_size = 1000,
validation_split = 0.3,
verbose = 1,
callbacks = [early_stop])
In [ ]:
model.evaluate(X_test, Y_test_cat)